// Listing 17.18 - writing a Class to a file
#include <fstream>
#include <iostream>
using namespace std;

class Animal
{
public:
    Animal(int weight, long days) :itsWeight(weight), DayAlive(days) {}
    ~Animal(){}

    int GetWeight() const { return itsWeight; }
    void SetWeight(int weight) { itsWeight = weight; }

    long GetDayAlive() const { return DayAlive; }
    void SetDayAlive(long days) { DayAlive = days; }
    private:
        int itsWeight;
        long DayAlive;
};

int main(void) 
{
    char fileName[80];
    cout << "Please enter the file name: ";
    cin >> fileName;

    ofstream fout(fileName, ios::binary);
    if(!fout)
    {
        cout << "Unable to open " << fileName << " for writing.\n";
        return 1;
    }

    Animal Bear(50, 100);
    fout.write((char*) &Bear, sizeof Bear);
    fout.close();

    ifstream fin(fileName,ios::binary);
    if(!fin)
    {
        cout << "Unable to open " << fileName << " for reading.\n";
        return 1;
    }

    Animal BearTwo(1, 1);
    cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
    cout << "BearTwo days: " << BearTwo.GetDayAlive() << endl;

    fin.read((char*) &BearTwo, sizeof BearTwo);
    cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
    cout << "BearTwo days: " << BearTwo.GetDayAlive() << endl;
    fin.close();
}